<template>
  <dt-modal-edit
    ref="modal"
    title="特殊规则设置"
    :label-width="110"
    :width="500"
    @ok="onOk"
    @cancelData="cancelData"
    :loading="loading"
    :okText="okText">
    <dt-info :label-width="150" ref="info3" style="width: 100%"></dt-info>
  </dt-modal-edit>
</template>

<script>
  import Opt from '../opt/specialRules'
  import sopApi from '@/biz/buildingManagement/sopConfig'
  export default {
    data() {
      return {
        okText: '确定',
        info: {
          sopName: '',
          delayDay: null,
          date: []
        },
        callback: null,
        loading: false,
        currOrg: {},
        currentRow: {},
        disFlag: false
      }
    },
    mounted() {},
    methods: {
      show(currVal, currRow) {
        this.currOrg = currVal
        this.currentRow = currRow
        this.info.sopName = this.currentRow.sopName
        this.specialRules()
      },
      cancelData() {
        this.info = {
          sopName: '',
          delayDay: null,
          date: []
        }
      },
      specialRules() {
        let params = {
          configId: this.currentRow.id,
          orgId: this.currOrg.orgId,
          level: this.currOrg.level
        }
        return sopApi
          .getRule(params)
          .then((res) => {
            if (res) {
              this.info.delayDay = res.delayDay
              this.info.date = [res.ruleStartTime, res.ruleEndTime]
              this.disFlag = true
              this.okText = '提前关闭'
            } else {
              this.disFlag = false
              this.okText = '确定'
            }
            this.$refs.modal.show()
            this.$refs.info3.init(new Opt(this.info, this))
            this.$refs.info3.resetFields()
          })
          .catch(() => {})
      },

      earlyShutdown1() {
        let params = {
          configId: this.currentRow.id,
          orgId: this.currOrg.orgId,
          level: this.currOrg.level
        }
        return sopApi
          .earlyShutdown1(params)
          .then((res) => {
            this.$refs.modal.hide()
            this.$Message.success('提前关闭成功')
            this.$emit('success')
            this.loading = false
          })
          .catch(() => {})
      },
      editRule() {
        if (!this.info.delayDay) {
          this.$Message.warning('请填写延长天数')
          return
        }
        if (this.info.delayDay < 1) {
          this.$Message.warning('延长天数只能为正整数')
          return
        }
        if (!this.info.date[0]) {
          this.$Message.warning('请选择生效日期')
          return
        }
        let params = {
          configId: this.currentRow.id,
          delayDay: this.info.delayDay,
          orgId: this.currOrg.orgId,
          level: this.currOrg.level,
          orgName: this.currOrg.orgName,
          ruleEndTime: this.info.date[1],
          ruleStartTime: this.info.date[0]
        }
        return sopApi
          .editRule1(params)
          .then((res) => {
            this.$refs.modal.hide()
            this.$Message.success('设置成功')
            this.$emit('success')
            this.loading = false
          })
          .catch(() => {})
      },
      hide() {
        this.$refs.modal.hide()
      },
      onOk() {
        this.loading = true
        if (this.disFlag) {
          this.earlyShutdown1()
        } else {
          this.editRule()
        }
      }
    }
  }
</script>

<style lang="scss" scoped></style>
